﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;


namespace WebControlApp
{
    static class Logic
    {
        public static void sendData(Stream TcpStream, byte[] PacketData, byte[] ReturnData)
        {
            
            int Data;
            double FracData;
            double distance = 0.0098;

            try
            {

                TcpStream.Write(PacketData, 0, PacketData.Length);
                TcpStream.Read(ReturnData, 0, ReturnData.Length);


                textBox2.Text = System.Text.Encoding.Default.GetString(ReturnData, 0, 1);
                //Left IR
                Data = ReturnData[1] << 8;
                Data += ReturnData[2];
                textBox2.Text = textBox2.Text + " " + ((uint)Data).ToString();
                if (Data > 500)
                {
                    //left IR
                    Data = ((int)((1462 / (Data - 348)) / 2.54));
                    
                }
                else
                {
                    //left IR
                    Data = ((int)((4836.9 / (Data - 16.116)) / 2.54));
                }

                //Right IR
                Data = ReturnData[3] << 8;
                Data += ReturnData[4];
                textBox2.Text = textBox2.Text + " " + ((uint)Data).ToString();
                if (Data > 500)
                {
                    Data = ((int)((1462 / (Data - 348)) / 2.54));
                }
                else
                {
                    Data = ((int)((4836.9 / (Data - 16.116)) / 2.54));
                }

                //Ultra Sonic
                Data = ReturnData[5] << 8;
                Data += ReturnData[6];
                textBox2.Text = textBox2.Text + " " + ((uint)Data).ToString();
                distance = ((((double)Data * 5) / 1024) / 0.0098);
                Data = (int)distance;
                if (Data <= 120)
                {
                    label16.Text = Data.ToString() + "in";
                    trackBar3.Value = Data;
                }
                else
                {
                    label16.Text = "120 in";
                    trackBar3.Value = 120;

                }
                //Left Motor Current
                Data = ReturnData[7] << 8;
                Data += ReturnData[8];
                textBox2.Text = textBox2.Text + " " + ((uint)Data).ToString();
                FracData = Math.Truncate((((((double)Data * 5) / 1024) / 100) * 37500)) / 100;
                label11.Text = FracData.ToString() + "A";
                //Right Motor Current
                Data = ReturnData[9] << 8;
                Data += ReturnData[10];
                textBox2.Text = textBox2.Text + " " + ((uint)Data).ToString();
                FracData = Math.Truncate((((((double)Data * 5) / 1024) / 100) * 37500)) / 100;
                label12.Text = FracData.ToString() + "A";
                //Left Distance
                Data = ReturnData[11] << 8;
                Data += ReturnData[12];
                textBox2.Text = textBox2.Text + " " + ((uint)Data).ToString();
                label10.Text = Data.ToString();

                //Right Distance
                Data = ReturnData[13] << 8;
                Data += ReturnData[14];
                textBox2.Text = textBox2.Text + " " + ((uint)Data).ToString();
                label13.Text = Data.ToString();
            }
            catch (NullReferenceException nr)
            {
                toolStripStatusLabel4.Text = nr.Message;
            }
            catch (SocketException Se)
            {
                toolStripStatusLabel4.Text = Se.Message;
            }
            catch (InvalidOperationException Ie)
            {
                toolStripStatusLabel4.Text = Ie.Message;
            }
            catch (IOException Io)
            {
                toolStripStatusLabel4.Text = Io.Message;
            }
        }

        public static void Connect(TcpClient Client,string CommandIP,string CommandPort,string VideoURL,Stream TcpStream)
        {
            try
            {
                Client.ReceiveTimeout = 500;
                Client.Connect(CommandIP, int.Parse(CommandPort));
                if (Client.Connected)
                {
                    TcpStream = Client.GetStream();
                    TcpStream.ReadTimeout = 500;
                    webBrowser1.Navigate(VideoURL);
                    toolStripStatusLabel4.Text = "Connected";
                    SyncTimer.Start();
                }
            }
            catch (SocketException Se)
            {
                toolStripStatusLabel4.Text = Se.Message;
            }
            catch (InvalidOperationException Ie)
            {
                toolStripStatusLabel4.Text = Ie.Message;
            }
        }

        public static void Disconnect(Stream TcpStream, TcpClient Client)
        {
            try
            {
                TcpStream.Close();
                Client.Close();

            }
            catch (NullReferenceException nr)
            {
                toolStripStatusLabel4.Text = nr.Message;
            }

            SyncTimer.Stop();
        }

    }
}
